//
//  SEGWKWebBottomView.m
//  Pods-SEGWebView_Example
//
//  Created by 山刘 on 2020/3/10.
//

#import "SEGWKWebBottomView.h"
#import "NSString+WKAdd.h"
#import "UIButton+EnlargeTouchArea.h"

@implementation SEGWKWebBottomView
-(instancetype)initWithFrame:(CGRect)frame{
    if (self == [super initWithFrame:frame]) {
        self.backgroundColor = UIColor.orangeColor;
        [self segSddSubviews];
    }
    return self;
}

-(void)segSddSubviews{
    [self addSubview:self.goBackButton];
    [self addSubview:self.goForwardButton];
    [self addSubview:self.lineView];
    
    [self addSubview:self.closeButton];
    [self addSubview:self.shareButton];
    [self addSubview:self.reloadButton];
    
    [self addSubview:self.testButton];
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    [self.lineView setFrame:CGRectMake(0,0, CGRectGetWidth(self.frame), 0.5)];
    
    // 关闭
    [self.closeButton sizeToFit];
    [self.closeButton setFrame:CGRectMake(15.F,
                                           (CGRectGetHeight(self.frame)-CGRectGetHeight(self.closeButton.bounds))*0.5,
                                           CGRectGetWidth(self.closeButton.bounds),
                                           CGRectGetHeight(self.closeButton.bounds))];
    // 刷新
    [self.reloadButton sizeToFit];
    [self.reloadButton setFrame:CGRectMake(CGRectGetWidth(self.frame) - CGRectGetWidth(self.reloadButton.bounds) - 15.F,
                                          (CGRectGetHeight(self.frame)-CGRectGetHeight(self.reloadButton.bounds))*0.5,
                                           CGRectGetWidth(self.reloadButton.bounds),
                                           CGRectGetHeight(self.reloadButton.bounds))];
    
    [self.shareButton sizeToFit];
    [self.shareButton setFrame:CGRectMake(CGRectGetMinX(self.reloadButton.frame) - CGRectGetWidth(self.shareButton.bounds) - 30.F,
                                          (CGRectGetHeight(self.frame)-CGRectGetHeight(self.reloadButton.bounds))*0.5,
                                           CGRectGetWidth(self.shareButton.bounds),
                                           CGRectGetHeight(self.shareButton.bounds))];
    
    
    
    [self.goBackButton sizeToFit];
    [self.goBackButton setFrame:CGRectMake(CGRectGetWidth(self.frame)*0.5 - CGRectGetWidth(self.goBackButton.bounds) - 40,
                                           (CGRectGetHeight(self.frame)-CGRectGetHeight(self.goBackButton.bounds))*0.5,
                                           CGRectGetWidth(self.goBackButton.bounds),
                                           CGRectGetHeight(self.goBackButton.bounds))];
    
    [self.goForwardButton sizeToFit];
    [self.goForwardButton setFrame:CGRectMake(CGRectGetWidth(self.frame)*0.5 + 40,
                                              (CGRectGetHeight(self.frame)-CGRectGetHeight(self.goForwardButton.bounds))*0.5,
                                              CGRectGetWidth(self.goForwardButton.bounds),
                                              CGRectGetHeight(self.goForwardButton.bounds))];
    
    [self.testButton sizeToFit];
    [self.testButton setFrame:CGRectMake((CGRectGetWidth(self.frame) - CGRectGetWidth(self.testButton.bounds))*0.5,
                                         (CGRectGetHeight(self.frame)-CGRectGetHeight(self.testButton.bounds))*0.5,
                                         CGRectGetWidth(self.testButton.bounds),
                                         CGRectGetHeight(self.testButton.bounds))];
    
    /*self.goBackButton.backgroundColor = [UIColor redColor];
    self.goForwardButton.backgroundColor = [UIColor redColor];
    self.closeButton.backgroundColor = [UIColor redColor];
    self.shareButton.backgroundColor = [UIColor redColor];
    self.reloadButton.backgroundColor = [UIColor redColor];*/

    [self.goBackButton setEnlargeEdgeWithTop:0 right:10 bottom:0 left:10];
    [self.goForwardButton setEnlargeEdgeWithTop:0 right:10 bottom:0 left:10];
    [self.closeButton setEnlargeEdgeWithTop:0 right:10 bottom:0 left:10];
    [self.shareButton setEnlargeEdgeWithTop:0 right:10 bottom:0 left:10];
    [self.reloadButton setEnlargeEdgeWithTop:0 right:10 bottom:0 left:10];
}

-(UIButton *)goBackButton
{
    if (!_goBackButton) {
        UIImage *image = [UIImage imageNamed:[NSString resourceName:@"back"]];
        image = [image imageWithRenderingMode:(UIImageRenderingModeAlwaysTemplate)];
        
        _goBackButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_goBackButton setImage:image forState:UIControlStateNormal];
        [_goBackButton setImage:image forState:UIControlStateDisabled];
        _goBackButton.enabled = NO;
        _goBackButton.tintColor = [UIColor grayColor];
    }
    return _goBackButton;
}

-(UIButton *)goForwardButton
{
    if (!_goForwardButton) {
        UIImage *image = [UIImage imageNamed:[NSString resourceName:@"forward"]];
        image = [image imageWithRenderingMode:(UIImageRenderingModeAlwaysTemplate)];
        
        _goForwardButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_goForwardButton setImage:image forState:UIControlStateNormal];
        [_goForwardButton setImage:image forState:UIControlStateDisabled];
        _goForwardButton.enabled = NO;
        _goForwardButton.tintColor = [UIColor grayColor];
    }
    return _goForwardButton;
}

-(UIView *)lineView{
    if (!_lineView) {
        _lineView = [[UIView alloc] init];
    }
    return _lineView;
}

-(UIButton *)closeButton
{
    if (!_closeButton) {
        UIImage *image = [UIImage imageNamed:[NSString resourceName:@"close"]];
        image = [image imageWithRenderingMode:(UIImageRenderingModeAlwaysTemplate)];
        
        _closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_closeButton setImage:image forState:UIControlStateNormal];
    }
    return _closeButton;
}

-(UIButton *)shareButton{
    if (!_shareButton) {
        UIImage *image = [UIImage imageNamed:[NSString resourceName:@"share"]];
        image = [image imageWithRenderingMode:(UIImageRenderingModeAlwaysTemplate)];
        
        _shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_shareButton setImage:image forState:UIControlStateNormal];
        [_shareButton setHidden:YES];
    }
    return _shareButton;
}

-(UIButton *)reloadButton{
    if (!_reloadButton) {
        UIImage *image = [UIImage imageNamed:[NSString resourceName:@"refresh"]];
        image = [image imageWithRenderingMode:(UIImageRenderingModeAlwaysTemplate)];
        
        _reloadButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_reloadButton setImage:image forState:UIControlStateNormal];
    }
    return _reloadButton;
}

-(UIButton *)testButton{
    if (!_testButton) {
        _testButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_testButton setTitle:@"测试" forState:UIControlStateNormal];
        [_testButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_testButton setBackgroundColor:[UIColor redColor]];
        [_testButton setHidden:YES];
    }
    return _testButton;
}
@end
